package com.guli.product.service.impl;

import com.guli.product.dao.PmsAttrDao;
import com.guli.product.dao.PmsCategoryDao;
import com.guli.product.entity.PmsAttrEntity;
import com.guli.product.entity.PmsCategoryEntity;
import com.guli.product.vo.AttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.product.dao.PmsAttrGroupDao;
import com.guli.product.entity.PmsAttrGroupEntity;
import com.guli.product.service.PmsAttrGroupService;


@Service("pmsAttrGroupService")
public class PmsAttrGroupServiceImpl extends ServiceImpl<PmsAttrGroupDao, PmsAttrGroupEntity> implements PmsAttrGroupService {

    @SuppressWarnings("all")
    @Autowired
    PmsAttrGroupDao pmsAttrGroupDao;

    @SuppressWarnings("all")
    @Autowired
    PmsCategoryDao categoryDao;

    @SuppressWarnings("all")
    @Autowired
    PmsAttrDao pmsAttrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PmsAttrGroupEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)){
            wrapper.eq("attr_group_name",key);
        }
        String catelogid = (String) params.get("catelogid");
        if (StringUtils.isNotEmpty(catelogid)){
            wrapper.eq("catelog_id",catelogid);
        }

        IPage<PmsAttrGroupEntity> page = this.page(
                new Query<PmsAttrGroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PmsAttrGroupEntity findOne(Long attrGroupId) {
        PmsAttrGroupEntity pmsAttrGroupEntity = pmsAttrGroupDao.selectById(attrGroupId);
        //获取三级节点的值
        Long catelogId = pmsAttrGroupEntity.getCatelogId();
        //定义一个集合存储一级到三级节点的id值
        ArrayList<Long> longs = new ArrayList<>();
        //调用收集方法(递归收集)
        addids(longs,catelogId);
        //将所有的值顺序掉换为一级 二级 三级
        Collections.reverse(longs);
        //将集合转换为数组
        Long[] longs1 = longs.toArray(new Long[longs.size()]);
        //将数组赋值为对象
        pmsAttrGroupEntity.setCatelogPath(longs1);
        return pmsAttrGroupEntity;
    }

    @Override
    public List<AttrGroupVo> attrGroupService(Long catalogId) {
        //1通过当前分类查询分组信息
        List<PmsAttrGroupEntity> catelog_id = pmsAttrGroupDao.selectList(new QueryWrapper<PmsAttrGroupEntity>().eq("catelog_id", catalogId));
        List<AttrGroupVo> collect = catelog_id.stream().map(item -> {
            //2遍历所有分组信息把每个分组的对象存入vo当中
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);
            //3现在没有值数据为每个小组的规格参数集合
            QueryWrapper<PmsAttrEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_type", 1);
            wrapper.eq("attr_group_id", item.getAttrGroupId());
            List<PmsAttrEntity> attrs = pmsAttrDao.selectList(wrapper);
            attrGroupVo.setAttrs(attrs);
            return attrGroupVo;
        }).collect(Collectors.toList());
        return collect;
    }

    public void addids(List<Long> list,Long catelogid){
        list.add(catelogid);
        PmsCategoryEntity pmsCategoryEntity = categoryDao.selectById(catelogid);
        if (!pmsCategoryEntity.getParentCid().equals(0L)){
            addids(list,pmsCategoryEntity.getParentCid());
        }
    }

}